package com.example.mymicaihu.fragment.home;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.mymicaihu.R;
import com.example.mymicaihu.adapter.home.NewsAdapter;
import com.example.mymicaihu.bean.home.NewsBean;
import com.example.mymicaihu.callback.Mainconcations;
import com.example.mymicaihu.model.MainModel;
import com.example.mymicaihu.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/3/29  14:30
 * 作用： 推荐
 */
public class NewsFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView {
    @BindView(R.id.rv_home_news)
    RecyclerView rv_list;
    private static final String TAG = "朱勇闯Video";
    private List<NewsBean.NewsListBean> newsList;
    private NewsAdapter newsAdapter;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        newsList = new ArrayList<>();
        rv_list.setLayoutManager(new LinearLayoutManager(context));
        newsAdapter = new NewsAdapter(context, newsList);
        rv_list.setAdapter(newsAdapter);
        HashMap<String, Object> map = new HashMap<>();
        map.put("cv", "2.5.3");
//        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg");
        map.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg==");
        map.put("referer", "http://api.micaiying.com");
        mPresenter.getData(map, "http://api.micaiying.com/", "news/newsList", 0);
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home_news;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
           /* Gson gson = new Gson();
            Type type1 = new TypeToken<List<VideoBean.NewsListDTO>>() {
            }.getType();
            videoList = gson.fromJson((JsonElement) t, type1);*/

            NewsBean newsBean = new Gson().fromJson((JsonElement) t, NewsBean.class);
            newsList = newsBean.getNewsList();
            newsAdapter = new NewsAdapter(context, newsList);
            rv_list.setAdapter(newsAdapter);
            newsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}
